<?php
// 本类由系统自动生成，仅供测试用途
class CustomerAction extends CommonAction {
	public function _filter(&$map) {
		$map['removed']=1;
		$map['id'] = array('not in ','1,2,3');
	}
	public function _before_insert() {
		$model = M ( "Customer" );
		if ($model->where ( "customername='" . $_POST ['customername']."'" )->select ()) { // 判断客户名是否已存在
			$this->ajaxReturn ( '1', '客户名称重复!', '300' );
			exit ();
		}
	}
	public function _before_update() {
		$model = M ( "Customer" );
		$customername = $model->find ( $_POST ['id'] );
		if ($customername ['customername'] != $_POST ['customername']) {
			if ($model->where ( array (
					'customername' => $_POST ['customername'] 
			) )->select ()) {
				$this->ajaxReturn ( '1', '客户名称重复!', '300' );
				exit ();
			}
		}
	}
	public function _before_edit() {
		$model = M ( "Customer" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function read(){
	    $model = M('Customer');
	    $id=$_REQUEST['id'];
	    $vo=$model->where('id='.$id)->find();
	    $this->assign('vo',$vo);
	    $this->display();
	}
	/**
	 * 查看供货信息
	 *
	 */
	public function readSupply() {
		$model = M ( "CustomerGood" );
		$id=$_REQUEST['id'];
		$vo = $model->where('customerid='.$id)->select();
		$this->assign('list',$vo);
		$this->display();
	}
	
	public function searchSupply() {
		$model = M ( 'CustomerSupply' );
		if (isset ( $_POST['id'] )) {
			$list = $model->where ( 'id=' .$_POST['id'] )->select ();
			$this->assign ( 'vo', $list );
		}
		$this->display ( 'readSupply' );
	}
	public function getcontract(){
		$customerid = $_SESSION ['id'];
		$model = M ( 'Customer' );
		$name = $model->query ( 'select customername from mlt_customer where mlt_customer.id=' . $customerid );
		$name1=$name[0]['customername'];
		$contract=M('Contract');
		$list1=$contract->where("type=1 and removed=1 and secondname='".$name1."'")->select();
		$this->assign ( 'list1', $list1 );
		$this->display();
	}
	public function payAdd() {
		if('true'==$_REQUEST['chooseReceiver']){
			$this->chooseReceiver();
		}else{
			if('true'==$_REQUEST['insertPay']){
				$this->insertPay();
			}else{
				$customerid = $_REQUEST ['id'];
				$_SESSION['id']	=$customerid;
				$this->assign ( 'customerid', $customerid );
				$model = M ( 'Customer' );
				$name = $model->query ( 'select * from mlt_customer where mlt_customer.id=' . $customerid );
				$this->assign ( 'vo', $name[0]);
				$this->display();
			}
		}
	}
	/**
	 * 插入收款记录
	 */
	public function insertPay() {
		$model = M ( 'CustomerPayment' );
		$model4 = M ( 'GatheringLog' );
		$contractno=$_POST['contractno'];
		$paymuch=$_POST['paymuch'];
		$cusid = $_POST['customerid'];
		$Conid = ($_POST['Conid']);
		$contract=M('Contract');
		$model1 = M ( 'Customer' );
		$model2 = M ( 'ControlTable' );
		$model->startTrans();//开启事务
		$RCT = true;
		$RConSave = true;
		$RCusSave = true;
		//$model3 = M ( 'CustomepaymentrUser' );
		$userid=explode(',',$_POST['userid']);
		$contractlist=$contract->where("contractno='".$contractno."'")->find();
		
		if($contractlist['wpayment']==null){
			if($paymuch>$contractlist['totalprice']){
				$model2->rollback();
				$this->ajaxReturn(1,'付款大于总额，请重新输入',300);
			}else{
				$data['apayment']=$contractlist['apayment']+$paymuch;
				$data['wpayment']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
				$RConSave =  $contract->where("contractno='".$contractno."'")->save($data);
 				//更客户为已经付款款
				$cusdata['payondelivery']=0;
				$RCusSave = $model1->where('id='.$cusid)->save($cusdata);
				//更改调度为已付款
				if($Conid!==""&&$Conid!==NULL){
					$RCT = $model2->where('id='.$Conid)->save($cusdata);
				}
				$_POST['haspay']=$contractlist['apayment']+$paymuch;
				$_POST['nopay']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
			}			
		}else{
			if($contractlist['wpayment']!=0){
				if($paymuch>$contractlist['wpayment']){
					$model2->rollback();
					$this->ajaxReturn(1,'付款大于未付款总额，请重新输入',300);
				}else{
					$data['apayment']=$contractlist['apayment']+$paymuch;
					$data['wpayment']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
					$RConSave = $contract->where("contractno='".$contractno."'")->save($data);
 					//更客户为已经付款款
					$cusdata['payondelivery']=0;
					$RCusSave = $model1->where('id='.$cusid)->save($cusdata);
					//更改调度为已付款
					if($Conid!==""&&$Conid!==NULL){
						$RCT = $model2->where('id='.$Conid)->save($cusdata);
					}
					$_POST['haspay']=$contractlist['apayment']+$paymuch;
					$_POST['nopay']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
				}				
			}else{
				$model2->rollback();
				$this->ajaxReturn(1,'款已付清！',300);
			}
		}	
		$_POST['alshipment']=0;
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 保存当前数据对象
		
		$dataGathering['customername']= $_REQUEST['payer'];
		$dataGathering['customerid']= $_REQUEST['customerid'];
		$dataGathering['seller']= $_REQUEST['seller'];
		$dataGathering['capacity']= $_REQUEST['capacity'];
		$dataGathering['total']= $_REQUEST['total'];
		$dataGathering['paytime']= $_REQUEST['paytime'];
		$dataGathering['paymuch']= $_REQUEST['paymuch'];
		$dataGathering['contractno']= $_REQUEST['contractno'];
		$controlid=$_REQUEST['controlid'];//合同id
		//$CPT = $model->where('controlid='.$controlid.' and status!=100')->select();
		$CPT = $model->where('controlid='.$controlid.' and  customerid= '.$cusid)->select();
		$k = count($CPT);
		if($k>0){
			$data1['capacity'] = bcadd($CPT[0]['capacity'],$_REQUEST['capacity'],4);
			$data1['paymuch'] = bcadd($CPT[0]['paymuch'],$_REQUEST['paymuch'],4); 
			$data1['status']=($CPT[0]['alshipment']/$data1['capacity'])*100;
			$data1['haspay']=$_POST['haspay'];
			$data1['nopay']=$_POST['nopay'];
			$list = $model->where('id='.$CPT[0]['id'])->save($data1);
			
			$dataGathering['customerpaymentid']= $CPT[0]['id'];
		}else{
			$list = $model->add ();
			$dataGathering['customerpaymentid']= $list;
		}
		$RGatLog = $model4->add($dataGathering);
		if (($RGatLog!==false)&&($list!==false)&&($RConSave!==false)&&($RCusSave!==false)&&($RCT!==false)) { // 保存成功
			$model2->commit();
// 			for($i=0;$i<count($userid);$i++){
// 				$data3['customerpaymentid']=$list;
// 				$data3['userid']=$userid[$i];
// 				$model3->add($data3);
// 			}
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->ajaxReturn ( 1, '付款录入成功!', 200 );
		} else {
			$model2->rollback();
			// 失败提示
			$this->ajaxReturn ( 1, '付款录入失败!', 300 );
		}
	}
	/**
	 * 读取付款记录
	 */
	public function readPay() {
		$custmoerid=$_REQUEST['id'];
		$model=M('GatheringLog');
		$name=$model->query('select customername from mlt_customer where mlt_customer.id='.$custmoerid);
		import ( '@.Org.Page' );
		$count = $model->where('customerid='.$custmoerid)->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('customerid='.$custmoerid)->page ( $pageNum . "," . $page->listRows )->select ();
		
		$this->assign('customername',$name[0]['customername']);
		$this->assign ( 'id', $custmoerid);
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//双击查看付款单条记录
	public function viewaPay() {
		$id=$_REQUEST['id'];
		$model=M('CustomerPayment');		
		$vo = $model->where('id='.$id)->find ();
		$name=$model->query('select customername from mlt_customer where mlt_customer.id='.$vo['customerid']);
		$this->assign('customername',$name[0]['customername']);
		$this->assign('vo',$vo);
		$this->display();
	}
	public function supplyEdit() {
		$id=$_REQUEST['id'];
		$Freight=M('CustomerGood');
		$result=$Freight->where('id='.$id)->find();
		$this->assign('vo',$result);
		$this->display();
	}
	public function supplyUpdate() {
	    $id=$_REQUEST['id'];
		$Freight=M('CustomerGood');
		    $data['product']=$_POST['product'];
		    $data['supplyamount']=$_POST['supplyamount'];
		    $data['hasexam']=$_POST['hasexam'];
		    $data['hassupply']=$_POST['hassupply'];
		    $data['nosupply']=$_POST['nosupply'];
			$result=$Freight->where('id='.$id)->save($data);
			if($result)
			{
				$this->ajaxReturn($result,'更新成功',200);
			}
			else
			{
				$this->ajaxReturn($result,'更新失败',300);
			}
}


public function _before_foreverdelete()
{
	$id = $_REQUEST ['ids'];
	$model1 = M('Customer');
	$model1->startTrans();//开启事务
	$model2 = M('CustomerPayment');
	$model3 = M('ContractLog');
	$result = true;
	$result3 = true;
	$result2 = true;
	$Cuspay = $model2->where('customerid='.$id)->select();
	//删除拟定调度
	if(count($Cuspay)>0){
		for($i=0;$i<count($Cuspay);$i++){
			$CTPlist = $model3->where('customerpaymentid='.$Cuspay[$i]['id'])->select();
			if(count($CTPlist)>0){
				$result3 = $model3->where('customerpaymentid='.$Cuspay[$i]['id'])->delete();
			}
		}
		//删除拟定审批
		$result2 = $model2->where('customerid='.$id)->delete();
	}
	$model=M('CustomerGood');
	$pk='customerid';
	if (isset ( $id )) {
		$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
		$RCusGood = $model->where ( $condition )->select();
		if(count($RCusGood)>0){
			$result = $model->where ( $condition )->delete ();
		}
		if (($result!==false) && ($result2!==false) && ($result3!==false)) {
			$model1->commit();
		} else {
			//$this->insertLog('彻底删除','操作'.$info.'编号为'.$id.'的数据失败');
			$model1->rollback();
			$this->ajaxReturn (1,'删除失败!',300);
		}
	} else {
		$model1->rollback();
		$this->ajaxReturn (1,'进行删除操作出现非法操作!',200);
	}
	 
}

public function getcon(){
	$customerid = $_SESSION ['id'];
	$model3 = M ( "Carrier" );
	$Carrier = $model3->select ();
	$model2 = M ( "Provider" );
	$Provider=$model2->select ();
	$model4 = M ( "Customer" );
	$Customer=$model4->select ();
	$this->assign ( 'carr', $Carrier );
	$this->assign ( 'provider', $Provider );
	$this->assign ( 'customer', $Customer );
	$contract=M('ControlTable');
	$list1=$contract->where('customer='.$customerid.' and payondelivery=1')->order('id desc')->select();
	$this->assign ( 'con', $list1 );
	$this->display();
}

//导出功能
public function export(){

	$model=D('Customer');
	 $customername=$_REQUEST['customername'];
	 $customertype=$_REQUEST['customertype'];
	// $sql="select * from mlt_customer where 1=1";
	$map['removed'] = array('neq',2);
	$map['id'] = array('not in ','1,2,3');
	if(!empty($customername)){
		$map['customername']=array('like','%'.$customername.'%');
		//$sql.=" and customername like '%" .$customername."%'";
	}
	 if(!empty($customertype)){
		$map['customertype']=array('like','%'.$customertype.'%');
		//$sql.=" and customertype like '%" .$customertype."%'";
	} 
 	$Customer=$model->where($map)->field('')->select();
 	//$Customer=$model->query($sql);

 	$cname=date('Y-m-d');
		
 	$map = array('客户号','客户类型','客户名称','业务员','电话','传真','网址','邮箱','城市','邮编','地址','开户银行','银行账号','税号','备注');
 	$field=array('customerno','customertype','customername','customermanager','phone','fax','site','email','city','zip','address','openbank','accountsno','taxno','notes');		
	$this->phpexcel($Customer, $cname, $map,$field);
		
}
/*
 * 付款后审批人带回
 */
public function chooseReceiver(){
	$User = M('User');
	$list=$User->select();
	$this->assign('list',$list);
	$this->display('chooseReceiver');
}


//删除付款记录
public function deletePay()
{
	$pid = $_REQUEST ['pid'];
	$id = $_REQUEST ['id'];
	$model = M ('GatheringLog');
	$model1=M('Contract');
	$cstomer=M('Customer');
	$cpt=M('CustomerPayment');
	// 开起事务
	$model->startTrans ();
	$list = $model->find ( $pid );
	//获得收款金额
	$paymuch=$list['paymuch'];
	//获得合同编号
	$contractno=$list['contractno'];
	//
	$customerpaymentid = $list['customerpaymentid'];
	$RCPT = true;
	$clist=$model1->where('contractno=\''.$contractno.'\'')->select();
	if(!empty($clist))
	{
		//获得合同总金额
		$total=$clist[0]['totalprice'];
		//获得已付款
		$apayment=$clist[0]['apayment'];
		//获得未付款
		$wpayment=$clist[0]['wpayment'];
		$cmap['apayment']=bcsub($apayment,$paymuch,4);
		$cmap['wpayment']=bcadd($wpayment,$paymuch,4);
		//$cmap['status']=0;
		$RCon = $model1->where('id='.$clist[0]['id'])->save($cmap);
		/*
		$lists=$model->where('customerid='.$id.' and contractno=\''.$clist[0]['contractno'].'\' and id>'.$pid)->order('id asc' )->select();
		//获得小于pid的总付款金额
	
		if(!empty($lists))
		{
			for ($i=0;$i<count($lists);$i++)
			{
				$cpmap['haspay']=$this->getCounts2($lists[$i]['id'],$pid,$id,$clist[0]['contractno']);
				$cpmap['nopay']=$total-$cpmap['haspay'];
				//$model->where('id='.$lists[$i]['id'])->save($cpmap);
			}
		}
		*/
		//相应的删除审批中的拟定审批的放量及调度等
		//有合同号获得审批放量的记录
		
		//$this->getCtLog($pid,$id);
		//获得客户的其他付款记录，如果不存在，修改customername表的状态
		$paylist=$model->where('customerid='.$id)->select();
		
		if(empty($paylist))
		{
			$cstomer=M('Customer');
			$csts=$cstomer->where('id='.$id)->find();
			$csmap['payondelivery']=1;
			$cstomer->where('id='.$id)->save($csmap);
		}
		
		//删除审批指定的人
	//	$cpuer=M('CustomepaymentrUser');
	//	$cre=$cpuer->where('customerpaymentid='.$pid)->delete();
		
		$cptlist = $cpt->where('id='.$customerpaymentid)->select();
		if(count($cptlist)>0){
			$data2['paymuch'] = bcsub($cptlist[0]['paymuch'],$list['paymuch'],4);
			$data2['capacity'] = bcsub($cptlist[0]['capacity'],$list['capacity'],4);
			$data2['status']=($cptlist[0]['alshipment']/$data2['capacity'])*100;
			$data2['haspay']=$cptlist[0]['haspay']-$paymuch;
			$data2['nopay']=$cptlist[0]['nopay']+$paymuch;
			if(bcsub($cptlist[0]['alshipment'],$data2['capacity'],4)>0){
				$model->rollback();
				$this->ajaxReturn(0,"删除后可审批吨数小于已审批吨数，不能删除",300);
			}
			$RCPT = $cpt->where('id='.$customerpaymentid)->save($data2);
		}
		
		
	  $re=$model->where('id='.$pid)->delete();
		//更新合同的审批状态
	  $cctmap['status']=$this->getres($clist[0]['id']);
	  $RConstatus = $model1->where('id='.$clist[0]['id'])->save($cctmap);
		 if(($re!==false)&&($RCPT!==false)&&($RConstatus!==false))
		{
			$model->commit();
			$this->ajaxReturn (1,'删除成功!',200);
		}
		else
		{
			$model->rollback();
			$this->ajaxReturn (1,'删除失败!',300);
		}
	}
	else
	{
		$model->rollback();
		$this->ajaxReturn (0,'没有对应改客户的合同!',300);
	}
	
}
//获得未还款的且id不在ids范围中且<id的借款累计
public function getCounts2($ids,$pids,$customerid,$contractno)
{
	$model=M('CustomerPayment');
	$list=$model->where('customerid='.$customerid.'  and contractno=\''.$contractno.'\' and id <='.$ids.' and id not in ('.$pids.')')->select();
	$totalcount=0;
	if(!empty($list))
	{
		for($i=0;$i<count($list);$i++){
			$money=$list[$i];
			$totalcount+=$money['paymuch'];
		}
	}
	return $totalcount;
}

//根据付款获得放量记录,并删除记录
public function getCtLog($contractno,$id)
{
	$ctlog=M('ContractLog');
	$ctlist=$ctlog->where('customerpaymentid=\''.$contractno.'\'')->select();
	if(!empty($ctlist))
	{
		for ($i=0;$i<count($ctlist);$i++)
		{
			$log=$ctlist[$i];
			//获得拟定调度的信息
			$ctable=M('ControlTable');
			$model3 = M('Unload');
			$ControlTable = $ctable->where('contractlogid='.$log['id'])->select();
			for($i=0;$i<count($ControlTable);$i++){
				$Unloaddata = $model3->getByOrders ( $ControlTable[$i] ['orders'] );
				if($Unloaddata){
					$this->ajaxReturn ( 0, "相关调度信息已经卸过车，不能删除调度信息！", 300);
				}
			}
			$cbllist=$ctable->where('contractlogid='.$log['id'])->select();
			if(!empty($cbllist))
			{
				for($j=0;$j<count($cbllist);$j++)
				{
					$table=$cbllist[$j];
					//删除对应的调度和卸车信息
					$this->UpwardReductionContractLog($table['id']);
					$this->controdelete($table['id']);
					//
					$ctable->where('id='.$table['id'])->delete();	
				}
			}
			//审批数
			$capacity=$log['capacity'];
			//删除调度人员信息
			 $ctloguser=M('ControllogUser');
			 $ctloguser->where('controllogid='.$log['id'])->delete();
			//删除放量记录信息
			//修改客户的商品信息
			 $custgood=M('CustomerGood');
			 $custgoods=$custgood->where('customerid='.$id.' and product=\''.$log['goodname'].'\'')->select();
			 
			 if(!empty($custgoods))
			 {
			 	$datagoods['hasexam']=bcsub($custgoods[0]['hasexam'],$capacity,4);
			 	$custgood->where('customerid='.$id.' and product=\''.$log['goodname'].'\'')->save($datagoods);
			 }
			 
			//修改商品的放量
			$cgood=M('ContractGood');
			//获得放量树
			$cgres=$cgood->where('id='.$log['contractgoodid'])->find();
			
			$cgmap['alshipment']=$cgres['alshipment']-$capacity;
			$cgmap['notshipment']=$cgres['quality']-$cgmap['alshipment'];
			$cgood->where('id='.$log['contractgoodid'])->save($cgmap);
			$ctlog->where('id='.$log['id'])->delete();
		}
	}
}

public function UpwardReductionContractLog($id){
	$model1 = M('ContractLog');
	$model2 = M ( 'ControlTable' );
	$ControlTableid = explode ( ',', $id );
	$k = count ( $ControlTableid );
	for($j = 0; $j < $k; $j ++) {
		$ControlTable = $model2->where ( 'id=' . $ControlTableid [$j] )->find ();
		//需要还原的吨数
		$tunnage = $ControlTable['Factorynetweight'];
		//上级拟定调度表的ID
		$contractlogid = $ControlTable['contractlogid'];
		if($contractlogid){
			$ContractLog = $model1->find($contractlogid);
			//拟定调度表可调度吨数
			$capacity = $ContractLog['capacity'];
			//拟定调度表已调度吨数
			$alshipment = $ContractLog['alshipment'];
			//收集信息
			$datactlog['alshipment']=bcsub($alshipment, $tunnage,4);//删除后调度的表已调度数增加
			$datactlog['status'] = ($datactlog['alshipment']/$capacity)*100;
			$model1->where('id='.$contractlogid)->save($datactlog);
		}
	}
}
	public function controdelete($id){
		$model1 = M ( 'ControlTable' );
		$model2 = M ( 'CarrierDispatch' );
		$model2->startTrans (); // 开启事务
		$model3 = M ( 'Unload' );
		$model4 = M ( "StoreManage" );
		if(is_array($id)){
			$condition = $id;
				
		}else{
			$condition = explode ( ',', $id );
		}
		$k = count ( $condition );
		for($j = 0; $j < $k; $j ++) {
			$ControlTable = $model1->where ( 'id=' . $condition [$j] )->find ();
			$shipper = $ControlTable ['shipper'];
			$start = $ControlTable['start'];//起运地
			$product = $ControlTable['product']; // 产品名称
			$sweight = $ControlTable['Factorynetweight'];//出厂净重
	
			// 还原供货商的剩余量
			$model = M ( 'Provider' );
			$Provider = $model->field ( 'id,providername,wshipment1,wshipment2,wshipment3' )->where ( 'id=\'' . $ControlTable ['provider'] . '\'' )->find ();
			//当供应商为仓库时
			//当供应商或者起运地为仓库时还原仓库吨数
			$result=true;
			if(($Provider['providername']=="东明库")||($start=='东明库')){
				$smdata = $model4->getByProduct ( $product );
				$storename='storeA';
				if ($smdata) {
					$Astoremanage = $model4->where ( 'product=\'' . $product . '\' and storename=\''.$storename.'\'' )->select ();
					$datage ['stock'] = bcadd($Astoremanage [0] ['stock'],$sweight,4);
				} else {
					$this->ajaxReturn ( '0', '东明库列表不存在' . $product . '相关物品!', '300' );
				}
			}else{
				if(($Provider['providername']=="南曹库")||($start=='南曹库')){
					$smdata = $model4->getByProduct ( $product );
					$storename='storeB';
					if ($smdata) {
						$Astoremanage = $model4->where ( 'product=\'' . $product . '\' and storename=\''.$storename.'\'' )->select ();
						$datage ['stock'] = bcadd($Astoremanage [0] ['stock'],$sweight,4);
					} else {
						$this->ajaxReturn ( '0', '南曹库列表不存在' . $product . '相关物品!', '300' );
					}
				}else{
					if(($Provider['providername']=="金海库")||($start=='金海库')){
						$smdata = $model4->getByProduct ( $product );
						$storename='storeC';
						if ($smdata) {
							$Astoremanage = $model4->where ( 'product=\'' . $product . '\' and storename=\''.$storename.'\'' )->select ();
							$datage ['stock'] = bcadd($Astoremanage [0] ['stock'],$sweight,4);
						} else {
							$this->ajaxReturn ( '0', '南曹库列表不存在' . $product . '相关物品!', '300' );
						}
					}else{
						if ($shipper == "普天") {
							$data ['wshipment1'] = $Provider ['wshipment1'] + bcsub($ControlTable ['eweight'],$ControlTable ['sweight'],4);
							$i = 1;
						} else {
							if ($shipper == "兰亭") {
								$data ['wshipment2'] = $Provider ['wshipment2'] + bcsub($ControlTable ['eweight'],$ControlTable ['sweight'],4);
								$i = 2;
							} else {
								if ($shipper == "天地") {
									$data ['wshipment3'] = $Provider ['wshipment3'] + bcsub($ControlTable ['eweight'],$ControlTable ['sweight'],4);
									$i = 3;
								}
							}
						}
					}
				}
			}
			$status = $model2->where ( 'orders=' . $ControlTable ['orders'] )->getField ( 'status' );
			$Unloaddata = $model3->getByOrders ( $ControlTable ['orders'] );
			// 删除对应的租车信息，当租车信息付过款式不能删除,除非用户为admin
			if ((! $Unloaddata && ('0' == $status))||('1'==$_SESSION [C ( 'USER_AUTH_KEY' )])) {
				if($data){
					$result1 = $model->where ( 'id=\'' . $ControlTable ['provider'] . '\'' )->save ( $data );
				}else{
					$result1=true;
				}
				if($datage){
					$model4->where ( 'product=\'' . $product . '\' and storename=\''.$storename.'\'' )->save($datage);
				}
				$result = $model2->where ( 'orders=' . $ControlTable ['orders'] )->delete ();
				//删除卸车记录
				$result3 = $model3->where ( 'orders=' . $ControlTable ['orders'] )->delete ();
				$model->commit (); // 提交事务
			} else {
				if ('1' == $status) {
					$this->ajaxReturn ( '0', "相关租车信息已经付过款，不能删除调度信息！", '300' );
				} else {
					if ($Unloaddata)
						$this->ajaxReturn ( '0', "相关调度信息已经卸过车，不能删除调度信息！", '300' );
				}
			}
		}
	}
	
	public function getres($id)
	{
		$cgood=M('ContractGood');
	
		$list=$cgood->where('contractid='.$id)->select();
		//$quality=0;
		if(!empty($list))
		{
			for ($i = 0; $i < count($list); $i++) {
				$quality+=$list[$i]['quality'];
				$alishiment+=$list[$i]['alshipment'];
			}
			if(!empty($quality)&&!empty($alishiment))
			{
				$precent=($alishiment/$quality)*100;
			}
			else
			{
				$precent=0;
			}
				
		}
		else
		{
			$precent=0;
		}
		return $precent;
	}
	

}